package it.unibo.refolding.pico.transformers;

import com.google.common.base.Preconditions;

public class ByteArray2ShortArrayStrategyByte implements Transformer {

  /**
   * Mask of the conversion target type.
   */
  private int mask;

  public ByteArray2ShortArrayStrategyByte(boolean signed) {
    this.mask = signed ? -1 : 0xFF;
  }

  @Override
  public short[] transform(byte[] input, double factor) {
    Preconditions.checkArgument((input.length & 1) == 0);
    int len = input.length >> 1;
    short[] s = new short[len];
    for (int i = 0; i < len; ++i) {
      byte a = input[i];
      int x = a & mask;
      double d = x / factor;
      Preconditions.checkArgument(Math.abs(d) <= Short.MAX_VALUE, d);
      s[i] = (short) d;
    }
    return s;
  }
}
